#!/usr/bin/env python
# Copyright (C) 2012 Google Inc. All Rights Reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import re
import subprocess
import sys

def GetAttachedDevices():
    re_device = re.compile('^([a-zA-Z0-9_:.-]+)\tdevice$', re.MULTILINE)
    process = subprocess.Popen(['adb', 'devices'], stdout=subprocess.PIPE)
    return re_device.findall(process.communicate()[0])


class AndroidDeviceStatus(object):
    def __init__(self, device_serial):
        self._device_serial = device_serial

    def _run_adb_command(self, command):
        full_command = ['adb', '-s', self._device_serial] + command
        stdout, _ = subprocess.Popen(full_command, stdout=subprocess.PIPE).communicate()
        return stdout.strip()

    def device_type(self):
        return self._run_adb_command(['shell', 'getprop', 'ro.build.product'])

    def device_build(self):
        return self._run_adb_command(['shell', 'getprop', 'ro.build.id'])

    def device_fingerprint(self):
        return self._run_adb_command(['shell', 'getprop', 'ro.build.fingerprint'])

    def battery_level(self):
        return self._run_adb_command(['shell', 'cat', '/sys/class/power_supply/battery/capacity'])

    def battery_temperature(self):
        temperature = self._run_adb_command(['shell', 'dumpsys', 'battery'])
        re_temperature = re.compile('temperature:\s+(\d+)')
        return re_temperature.findall(temperature)[0]

    def username(self):
        username = self._run_adb_command(['shell', 'id'])
        re_username = re.compile('uid=\d+\((.+?)\)')
        return re_username.findall(username)[0]


def main():
    devices = GetAttachedDevices()
    for device_serial in devices:
        device_status = AndroidDeviceStatus(device_serial)

        print 'Device %s (%s)' % (device_serial, device_status.device_type())
        print '  Build: %s (%s)' % (device_status.device_build(), device_status.device_fingerprint())
        print '  Battery: %s%%' % device_status.battery_level()
        print '  Battery temp: %s' % device_status.battery_temperature()
        print '  Username: %s' % device_status.username()
        print ''


if __name__ == '__main__':
    sys.exit(main())
